package com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad

import android.annotation.SuppressLint
import android.app.Activity
import com.anythink.interstitial.api.ATInterstitial
import com.anythink.interstitial.api.ATInterstitialAutoAd
import com.anythink.interstitial.api.ATInterstitialAutoEventListener
import com.bp.hearthealth.pulse.lnapps.ads.admob.open_ad.AppForegroundHelper
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.topon.ATAdSourceStatusListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.auto_load.ad_action.ATInterstitialAutoEventListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.auto_load.ad_action.TopOnInterstitialAutoAdListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.auto_load.ad_load.ATInterstitialAutoLoadListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.auto_load.ad_load.TopOnInterstitialAutoLoadListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.manual_load.ATInterstitialExListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.manual_load.TopOnInterstitialAdListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_source.TopOnAdSourceListener
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_utils.DateUtils

object TopOnInterAdManager {

    private var sourceStatusListenerImpl: ATAdSourceStatusListenerImpl? = null
    private var autoEventListenerImpl: ATInterstitialAutoEventListenerImpl? = null
    private var autoLoadListener: ATInterstitialAutoLoadListenerImpl? = null
    private var manualListener: ATInterstitialExListenerImpl? = null
    private var topOnInterstitialAdListener: TopOnInterstitialAdListener? = null

    @SuppressLint("StaticFieldLeak")
    var mInterstitialAd: ATInterstitial? = null

    fun setSourceListener(topOnAdSourceListener: TopOnAdSourceListener?) {
        if (sourceStatusListenerImpl == null) {
            sourceStatusListenerImpl = ATAdSourceStatusListenerImpl()
        }
        sourceStatusListenerImpl?.setSourceStatusListener(topOnAdSourceListener)
    }

    fun isAdReady(): Boolean {
        if (mInterstitialAd != null) {
            LogUtil.iTopOn("TopOn 【插页广告】 isAdReady：${mInterstitialAd!!.isAdReady}")
            return mInterstitialAd!!.isAdReady
        }

        LogUtil.iTopOn("TopOn 【插页广告】 mInterstitialAd == null")
        return false
    }

    fun showInterstitialAd(activity: Activity) {
        if (!AdsSwitch.getBoolean(AdsSwitch.adInterControl)) {
            topOnInterstitialAdListener?.onFireBaseAdIsClosed()
            return
        }
        if (isAdReady()) {
            mInterstitialAd?.show(activity)
        } else {
            topOnInterstitialAdListener?.onNotReady()
        }
    }

    fun setManualLoadListener(topOnInterstitialAdListener: TopOnInterstitialAdListener?) {
        if (manualListener == null) {
            manualListener = ATInterstitialExListenerImpl()
        }
        this.topOnInterstitialAdListener = topOnInterstitialAdListener
        manualListener!!.setManualLoadListener(topOnInterstitialAdListener)
    }

    fun initManualLoadInterstitialAd(activity: Activity) {
        if (manualListener == null) {
            manualListener = ATInterstitialExListenerImpl()
        }
        if (mInterstitialAd == null) {
            mInterstitialAd = ATInterstitial(activity, Constants.TOP_ON_INTER_ID)
        }
        if (sourceStatusListenerImpl == null) {
            sourceStatusListenerImpl = ATAdSourceStatusListenerImpl()
        }
        mInterstitialAd!!.setAdListener(manualListener)
//        mInterstitialAd!!.setAdSourceStatusListener(sourceStatusListenerImpl)
    }

    fun manualLoadAd() {
        if (!AdsSwitch.getBoolean(AdsSwitch.adInterControl)) {
            topOnInterstitialAdListener?.onFireBaseAdIsClosed()
            return
        }
        if (mInterstitialAd != null) {
            if (mInterstitialAd!!.checkAdStatus().isLoading) {
                LogUtil.iTopOn("【插页广告】正在加载中。。。。。。。。。")
            } else {
                LogUtil.iTopOn("TopOn 【插页广告】 ~~~~ 【开始时间】： ${DateUtils.nowTime(System.currentTimeMillis())}")
                mInterstitialAd?.load()
            }
        } else {
//            if (AppForegroundHelper().getCurrentActivity()!= null) {
//                if (AppForegroundHelper().getCurrentActivity() is BpMainActivity){
//                    initManualLoadInterstitialAd()
//                }
//            }
            LogUtil.iTopOn("TopOn 【插页广告】 mInterstitialAd == null")
        }
    }

    fun releaseManualLoadInterstitialAd() {
        if (mInterstitialAd != null) {
            setManualLoadListener(null)
            mInterstitialAd!!.setAdListener(null)
            mInterstitialAd!!.setAdSourceStatusListener(null)
            manualListener = null
            mInterstitialAd = null
        }
    }


    /**
     * 自动加载
     * 建议主Activity中创建，如在主Activity创建之前使用，Activity销毁后需要调用releaseAutoLoadInterstitialAd()释放资源防止泄漏
     *
     * TopOnInterAdManager.setAutoLoadListener(object : TopOnInterstitialAutoLoadListener {
     *             override fun onInterstitialAutoLoadFail(placementId: String?, adError: AdError?) {
     *                 LogUtil.iTopOn("TopOn 【插页广告】 自动加载失败")
     *             }
     *
     *             override fun onInterstitialAutoLoaded(placementId: String?) {
     *                 LogUtil.iTopOn("TopOn 【插页广告】 自动加载 加载成功")
     *                 TopOnInterAdManager.setAutoEventListener(object : TopOnInterstitialAutoAdListener {
     *                     override fun onInterstitialAdShow(adInfo: ATAdInfo) {
     *                         LogUtil.iTopOn("TopOn 【插页广告】 自动加载 展示成功")
     *                     }
     *
     *                     override fun onInterstitialAdClose(adInfo: ATAdInfo) {
     *                         LogUtil.iTopOn("TopOn 【插页广告】 自动加载 关闭")
     *                     }
     *                 })
     *                 if (TopOnInterAdManager.isAdReady()) {
     *                     ATInterstitialAutoAd.show(
     *                         this@BpMainActivity,
     *                         Constants.TOP_ON_Interstitial,
     *                         TopOnInterAdManager.getAutoEventListenerImpl()
     *                     )
     *                 }
     *                 LogUtil.iTopOn("TopOn 【插页广告】 自动加载成功")
     *             }
     *         })
     *         TopOnInterAdManager.initAutoLoadInterstitialAd(this@BpMainActivity)
     *
     */

    fun setAutoLoadListener(topOnInterstitialAutoAdListener: TopOnInterstitialAutoLoadListener?) {
        if (autoLoadListener == null) {
            autoLoadListener = ATInterstitialAutoLoadListenerImpl()
        }
        autoLoadListener!!.setAutoLoadListener(topOnInterstitialAutoAdListener)
    }

    fun setAutoEventListener(topOnInterstitialAutoAdListener: TopOnInterstitialAutoAdListener?) {
        if (autoEventListenerImpl == null) {
            autoEventListenerImpl = ATInterstitialAutoEventListenerImpl()
        }
        autoEventListenerImpl!!.setAutoEventListener(topOnInterstitialAutoAdListener)
    }

    fun getAutoEventListenerImpl(): ATInterstitialAutoEventListener? {
        return autoEventListenerImpl
    }

    fun initAutoLoadInterstitialAd(activity: Activity) {
        if (mInterstitialAd == null) {
            mInterstitialAd = ATInterstitial(activity, Constants.TOP_ON_INTER_ID)
        }
        ATInterstitialAutoAd.init(
            activity,
            arrayOf(Constants.TOP_ON_INTER_ID),
            autoLoadListener
        )
    }

    fun releaseAutoLoadInterstitialAd() {
        if (mInterstitialAd != null) {
            setSourceListener(null)
            setAutoLoadListener(null)
            setAutoEventListener(null)
            mInterstitialAd!!.setAdListener(null)
            mInterstitialAd!!.setAdDownloadListener(null)
            mInterstitialAd!!.setAdSourceStatusListener(null)
            mInterstitialAd = null
        }
    }
}

